/**
 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef PANDA_TOOLING_TEST_JS_SET_RESTART_FRAME_TEST_H
#define PANDA_TOOLING_TEST_JS_SET_RESTART_FRAME_TEST_H

#include "test_util.h"
#include "plugins/ecmascript/runtime/include/tooling/pt_ecmascript_extension.h"

namespace ark::tooling::test {
using JSTaggedValue = ark::ecmascript::JSTaggedValue;

class JsRestartFrameTest : public ApiTest {
public:
    JsRestartFrameTest()
    {
        vmStart = [this] {
            // NOLINTNEXTLINE(readability-magic-numbers)
            location_ = TestUtil::GetLocation("RestartFrame.js", 35, pandaFile_.c_str());
            ASSERT_TRUE(location_.GetMethodId().IsValid());
            return true;
        };

        breakpoint = [this](PtThread thread, Method *, const PtLocation &location) {
            ASSERT_TRUE(location.GetMethodId().IsValid());
            ASSERT_LOCATION_EQ(location, location_);
            ++breakpointCounter_;
            TestUtil::SuspendUntilContinue(DebugEvent::BREAKPOINT, thread, location);
            if (breakpointCounter_ == 1) {
                ASSERT_SUCCESS(debugInterface->RestartFrame(thread, 2));
            }
            if (breakpointCounter_ == 2) {
                ASSERT_SUCCESS(debugInterface->RestartFrame(thread, 0));
            }
            return true;
        };

        loadModule = [this](std::string_view moduleName) {
            if (moduleName.find(pandaFile_.c_str()) == std::string_view::npos) {
                return true;
            }
            ASSERT_SUCCESS(debugInterface->SetBreakpoint(location_));
            return true;
        };

        methodEntry = [this](PtThread, Method *) {
            ++entryExitCounter_;
            return true;
        };

        methodExit = [this](PtThread, Method *method, bool, VRegValue val) {
            auto moduleName = method->GetFullName();
            if (moduleName == "_GLOBAL::func_2") {
                // Force exit always zero
                auto taggedValue = ecmascript::PtEcmaScriptExtension::VRegValueToTaggedValue(val);
                if (taggedValue.IsInt()) {
                    result_ = taggedValue.GetInt();
                }
            }

            --entryExitCounter_;
            return true;
        };

        scenario = [this]() {
            ASSERT_BREAKPOINT_SUCCESS(location_);
            TestUtil::Continue();

            ASSERT_BREAKPOINT_SUCCESS(location_);
            TestUtil::Continue();

            ASSERT_BREAKPOINT_SUCCESS(location_);
            TestUtil::Continue();

            ASSERT_EXITED();
            return true;
        };

        vmDeath = [this] {
            // result_ indicate count of calls
            // frame0 +1
            // frame1 +10
            // frame2 +100    +100000
            // frame3 +1000   +100000
            // frame4 +10000  +100000
            ASSERT_EQ(result_, 322311);
            ASSERT_EQ(entryExitCounter_, 0U);
            ASSERT_EQ(breakpointCounter_, 3U);

            return true;
        };
    }

    std::pair<const char *, const char *> GetEntryPoint() override
    {
        return {pandaFile_.c_str(), entryPoint_.c_str()};
    }

private:
    std::string pandaFile_ = "js/RestartFrame.abc";
    std::string entryPoint_ = "_GLOBAL::func_main_0";
    PtLocation location_ {nullptr, PtLocation::EntityId(0), 0};
    size_t breakpointCounter_ = 0;
    size_t entryExitCounter_ = 0;
    int64_t result_ = 0;
};

inline std::unique_ptr<ApiTest> GetJsRestartFrameTest()
{
    return std::make_unique<JsRestartFrameTest>();
}
}  // namespace ark::tooling::test

#endif  // PANDA_TOOLING_TEST_JS_SET_RESTART_FRAME_TEST_H
